<?php
// ini_set("error_reporting", E_ALL);
// ini_set("display_errors","1"); 
// ini_set("ignore_repeated_errors","1");
// ini_set("log_errors", "1" ); 
// ini_set("error_log", "errors/errors_annotation.txt" ); 

	include ('php/Lib_Liste.php');
	include ('php/Lib_Anote.php');
	include ('php/Lib_Write.php');

	$nom = '';
	$texte='';
	$note ='';
	$donnees = '';
	$source='';
	$sources='';

	if (isset($_POST['nom'])) {$nom = $_POST['nom'];}
	if (isset($_POST['texte'])) {$texte = $_POST['texte'];}
	if (isset($_POST['note'])) {$note = $_POST['note'];}
	if (isset($_POST['action'])) { $action = $_POST['action']; }
	else {$action = "";}
	if (isset($_POST['num'])) {$num = $_POST['num'];}
	if (isset($_POST['delete'])) {$delete = $_POST['delete'];}
	if (isset($_POST['delete'])) { $action = $_POST['delete']; }

	
	$texte = stripslashes ($texte);
	
	if (file_exists("data/note/$nom.txt")) {
		$array_note = note2array($nom);
		$nb_note = sizeOf($array_note);
		
		for ($i=0;$i!=$nb_note;$i++){
			if (isset($_POST[$i])) {$source[$i] = $_POST[$i];}
			else {$source[$i] = NULL;}
		}
		for ($i=0;$i!=sizeOf($source);$i++){	
			if ($source[$i]!=NULL){
				$sources.="$source[$i]::";
			}
		}
	}
	else $sources = "::";

	switch ($action) {
	case "Ajouter":
    		$state = write_note ($nom,$texte,$note,$sources);
    		break;
	case "Modifier":
		$state = modify_note ($nom,$num,$texte,$note,$sources);
		break;
	case "Effacer" :
		$state = delete_note($nom,$num);
		break;
	}
	
	switch ($state) {
	case 0:
		$result = "L'annotation n'a pas ete ajoute! L'annotation d'annotation est une aberation, reflechissez un peu !!";
		break;
	case 1:
		$result = "L'annotation a ete ajoute! Pour visualiser les changements veuillez actualiser la page";
		break;
	case 2:
		$result = "L'annotation a ete effacer! Pour visualiser les changements veuillez actualiser la page";
		break;	
	}
	
	echo "	<html>
		<head>
			<script language='JavaScript' type='text/javascript'>
				function RefreshAndClose() {
					if (!window.opener.closed) {
						window.opener.location = window.opener.location;
						parent.close();
					}
				}
			</script>
		</head>
		<body>
		<center>
			<h1>$result</h1>
			<form> 
				<input type='button' onclick='RefreshAndClose()' value='Fermer la fenetre'>
			</form>
		</center>
		</body>
		</html>";
?>
